<?php

	/**
	* @name Issa PHP Timer
	* @version 1.0
	* @abstract Calculates script's execution times
	**/

	define('TPHP_TIMER_PRECISION', 6); // Zero means no rounding
	define('TPHP_TIMER_EXCESSIVE', 30); // Seconds
	define('TPHP_TIMER_ADDRESS', 'max@dreamsoft.us');

	class IssaTimer
	{
		private $m_iStartTime = array();
		private $m_iEndTime = array();
		private $m_mail = "";

		public function start($id = "")
		{
			$this->m_iStartTime[$id] = microtime(true);
		}

		public function stop($id = "")
		{
			$this->m_iEndTime[$id] = microtime(true);
		}

		public function get($id = "")
		{
			if (isset($this->m_iStartTime[$id])) {
				$iTotalTime = $this->m_iEndTime[$id] - $this->m_iStartTime[$id];
			} else {
				$iTotalTime = $this->m_iEndTime[$id] - $this->m_iStartTime[$id];
			}

			if($iTotalTime >= TPHP_TIMER_EXCESSIVE && !empty($this->m_mail))
			{
				@mail($this->m_mail, 'Warning: Execution Time', 'The execution time took longer than expected. ' . $id);
			}

			if(TPHP_TIMER_PRECISION == 0)
			{
				return $iTotalTime;
			}

			return round($iTotalTime, TPHP_TIMER_PRECISION);
		}

		public function getAll()
		{
			$_ = array();
			foreach ($this->m_iEndTime as $timer => $v) {
				$_[$timer] = $this->get($timer);
			}
			return $_;
		}
	}

	$pTimer = new IssaTimer();

?>